home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / start32.zip / START32.TXT < prev   
Text File  |  1993-04-13  |  15KB  |  317 lines

  1.  
  2.   Hi folks, this is Tran... This text explains START32, my 386 protected
  3. mode header... I'm assuming youre an assembler programmer. If you are not,
  4. this thing will do you no good. I'm also assuming you have some experience,
  5. and knowledge of the 386 wouldn't hurt either... If you don't know the 386,
  6. I suggest you get a good book if you really want to code it...
  7.  
  8.  
  9. First of all:
  10. -------------
  11.    I coded START32 for myself, it is designed and optimized for my needs. The
  12. greatest of which is the need for speed... That is the reason I do not
  13. implement paging in this thing, too slow... Also, since the only real mode
  14. INTs I usually need are DOS file functions, the V86 handler is not perfect,
  15. but it does DOS just fine.
  16.  
  17.  
  18. What it does:
  19. -------------
  20.    START32 is a piece of code that handles most of the things associated with
  21. using and getting into and out of 32bit protected mode. It also allows your
  22. protected mode programs to access the regular real-mode DOS and BIOS functions.
  23. START32 will check mundane little things like the processor and memory, then
  24. if the system is 386+ and not already in protected mode, it will switch the
  25. system into 32bit protected mode. In protected mode the system will run in
  26. flat memory, that is you won't need to bother with segment registers, and
  27. large data arrays will be much easier to handle...
  28.  
  29.  
  30. How the system works:
  31. ---------------------
  32.    After START32 initializes protected mode, it will jump to a label '_main',
  33. which mush be declared public in your code. This is the starting point of your
  34. program. All of the code and data in your program will reside in one large
  35. segment called 'code32' ... this is a 4gig segment. You do regular code using
  36. the 386 instruction set. To access a real-mode interrupt function, you load
  37. up 'virtual registers' with the values you want passed to the interrupt, then
  38. you load up AL with the number of the interrupt you wanna call, then you call
  39. INT 30h. The system will use the 386's v86 mode to call the real mode int.
  40. After which, the values in the 'virtual regs' will be what the interrupt
  41. handler passed back (All the real registers are preserved).
  42.  
  43.  
  44. Selectors:
  45. ----------
  46.    Instead of physical segment pointers, in protected mode, the 386 uses
  47. selectors. Selectors are indexes into a table of data that describe the
  48. memory location the specific index points to. But you need not concern
  49. yourself with these tables (GDT, IDT, LDTs)... This thing takes care of them.
  50. All you need to know, is that there are 3 basic selectors available to you.
  51. 8, 10h, and 18h... 8 is the selector for the code segment, this is the 4gig
  52. segment which you put all your flat code into. 10h is the selector for the
  53. data segment, the memory is physically the same as the code segment, but
  54. it must be done this way. That is 8 and 10h point to the same large segment,
  55. but CS must use 8, and the data segregs must use 10h... 18h is a data
  56. selector that points to the beginning of memory. 8 and 10h both start at the
  57. beginning of the segment 'code32' in memory, wherever it may be. But 18h
  58. always starts at absolute physical adress 0000:0000... This is in case you
  59. want to access something that resides before the beginning of the 32bit code,
  60. like the BIOS data area or the PSP... Upon arrival at '_main', CS ofcourse is
  61. 8. DS, ES, FS, and SS are 10h. And GS is 18h. And you should keep them this
  62. way because START32, whose job is by far not done after it passes control to
  63. your code, expects it this way.
  64.  
  65.  
  66. IRQs:
  67. -----
  68.    START32 handles IRQs for both the real system, and your protected mode code.
  69. By default, all the IRQ functions point to a piece of code in START32 which
  70. redirects them to their real-mode handler. You can set your own protected mode
  71. IRQ handlers, and control will pass DIRECTLY to them when they are called.
  72. There are four possible scenarios between the type of IRQ handler and the
  73. mode the IRQ was called in. The handling procedure can be the original real
  74. mode handler, or your own protected mode handler. And the IRQ can occur in
  75. protected mode, or when the system is servicing a V86 real mode INT. For
  76. this reason, your protected mode IRQ handler should make no assumption about
  77. the state of the system. That is, upon entry your handler, the only things
  78. you can be sure of, is CS=8, and SS=10h... You must set the other segregs
  79. manually if you wish to use them. Also, you must remember to terminate your
  80. IRQ routine with an 'IRETD' instead of an 'IRET'. If you wish to call the
  81. original real mode handler from your protected mode handler, simply call the
  82. real mode int associated with that IRQ from your handler (I would not suggest
  83. calling the original handler though. Because of the way START32 is coded, this
  84. will screw up your IRQ handler's stack if the initial IRQ ocurred in V86 mode).
  85.  
  86. BTW: No real mode interrupt vektor table entries are modified by START32, all
  87.  the IRQs go through the IDT.
  88.  
  89. Also: START32 will mask off IRQ 0 and 1 when it first starts. The system timer
  90.  and keyboard IRQs, because there is really no use for them untill you put
  91.  them to use.
  92.  
  93.  
  94. Memory:
  95. -------
  96.    Like I said, the whole system uses flat memory when you use START32. There
  97. are 2 types, low memory - below 640k, and high memory - above 1M... START32
  98. does some very basic 'memory management', if you could call it that. You
  99. can request blocks of a certain size, and START32 will either give ya a
  100. pointer to the block, or tell ya theres not enough of that type of memory.
  101. The blocks are allocated from the bottom up. You cannot free a memory below
  102. an area that you allocated later. You CAN free memory though, simply by
  103. decreasing the 'base of memory' pointer START32 uses to track the current
  104. base of usable memory. This pointer (along with a few others) is made available
  105. to your program. One good way of using this in a function, is along with any
  106. registers you PUSH, you also PUSH the pointer to the type of memory you
  107. wanna use. After the function is done and there is no more use for the memory
  108. it has allocated, you POP the pointer. If you need any memory area permanently
  109. throught the program, you should allocate it at the beginning, and ofcourse
  110. never decrease the pointer beyond this area.
  111.  
  112.  
  113. Crashes:
  114. --------
  115.    One thing about coding protected mode, you do not have the use of a
  116. debugger in finding bugz... Youre gonna have to make due with your own traps
  117. and internal debug aids... But a crash in protected mode is not as bad as in
  118. real mode. START32 handles the 386 exceptions. If any type of exception
  119. occurs, START32 will stop the program, do a debug dump of the exception
  120. number, registers, and stack ... and quit to DOS... The system will very
  121. rarely lock up, and the DOS environment will hardly ever be screwed up...
  122. Some reasons are that like I said, no real mode interrupt vektors are
  123. modified, and any area below the beginning of your program is usually only
  124. accessible through GS. This means that when your code goes astray, it cant
  125. overwrite DOS of the int vect table, or any TSRs below it, or anything. And
  126. there is hardly ever anything that can be screwed up above a program. An
  127. exception will usually occur very soon after a screwup in your code. Two of
  128. the most common exceptions are:
  129.  
  130. 6   - an invalid opcode. Either something overwrote your code, or you code
  131.       jumped off into limbo...
  132. 0dh - a general protection violation. Many things can cause this.
  133.  
  134.  
  135. The system itself:
  136. ------------------
  137.    First of all, START32 expects: CS=8, DS=ES=SS=FS=10h, GS=18h, and the
  138. direction flag cleared when you call any of its functions.
  139.  
  140.  
  141. In the included source code, at the top, the values are:
  142.  
  143.   EXTMIN - the minimum amount of extended memory that has to be present
  144.            for START32 to allow the program to run. (START32 will also quit
  145.            if it finds less than 639k low memory).
  146.  
  147.   TSTAK  - the total stack space for START32 to give. The default value of
  148.            400h bytes is usually enough, unless you plan to do recursion.
  149.  
  150.   ISTAK  - the amount of stack space to be given to a protected mode IRQ
  151.            handler when an IRQ comes from V86 mode. (This also includes
  152.            START32's exception handlers).
  153.  
  154.  
  155. START32 detects and will not run if a VGA is not present. This is not some
  156. system critical thing, but like I said, I wrote START32 for myself, and since
  157. I dont code anything that doesnt require a VGA right now, this is technically
  158. part of the system for me.
  159.  
  160.  
  161. START32 makes these variables available to your program:
  162.  
  163.   _totalextmem:word - The total amount of extended memory in the system (in K).
  164.  
  165.   _code16a:dword - Offset of the beginning of the program from absolute 0.
  166.                    This starts 100h bytes above the PSP.
  167.  
  168.   _code32a:dword - Offset of the beginning of protected mode code from
  169.                    absolute 0. This is useful for creating realative pointers
  170.                    to physical locations. That is if you wanted a pointer to
  171.                    the beginning of the VGA grafix area, realative to DS,
  172.                    move 0A0000h into a reg, then subtract _code32a from it.
  173.                    theres a macro provided to create such pointers, @rlp.
  174.  
  175.   _hextbl:bytes - Simply '0123456789ABCDEF'. It is used during an exception
  176.                   dump, but since it's there, might as well make it public
  177.                   since its so useful in printing hex numbers.
  178.  
  179.   _lomembase:dword - Current base of low memory (as a realative pointer, like
  180.                      all the other regular pointers in your code)... this is
  181.                      the beginning of free low memory that has not been
  182.                      allocated.
  183.  
  184.   _lomemtop:dword - Top of low memory as a realative pointer.
  185.  
  186.   _himembase:dword - Base of high memory, just like _lomembase.
  187.  
  188.   _himemtop:dword - Guess...
  189.  
  190.  
  191.   v86r_ax, v86r_bx, v86r_cx, v86r_dx, v86r_si, v86r_di, v86r_bp, v86r_ds,
  192.   v86r_es, v86r_ah, v86r_al, v86r_bh, v86r_bl, v86r_ch, v86r_cl, v86r_dh,
  193.   v86r_dl, v86r_flags
  194.  
  195.     These are the 'virtual registers' you use to communicate with real-mode
  196.     ints... v86r_flags though is used only to pass BACK flags, flags are not
  197.     set to it before invoking the V86 int...
  198.  
  199.  
  200. Here are some 'functions':
  201.  
  202. ------------------------------------------------------------------------------
  203.   _exit - Exit to real mode.
  204.  
  205. ------------------------------------------------------------------------------
  206.   _ret - Simply points to a RET instruction.
  207.  
  208. ------------------------------------------------------------------------------
  209.   _getvect - Get interrupt vektor, vektors 0-1fh and 30h are reserved, 31h+
  210.              dont exist, but 20h-2fh are IRQ's 0-0fh.
  211.     In:
  212.       BL - interrupt number
  213.     Out:
  214.       EAX - 32 bit offset in code
  215.  
  216. ------------------------------------------------------------------------------
  217.   _setvect - Set interrupt vektor.
  218.     In:
  219.       BL - interrupt number
  220.       EAX - 32 bit offset in code
  221.  
  222. ------------------------------------------------------------------------------
  223.   _getlomem - Get a low memory block.
  224.     In:
  225.       EAX - size requested
  226.     Out:
  227.       CF=1  - not enough mem  (carry flag set)
  228.         EAX - ?
  229.       CF=0  - memory allocated  (carry flag clear)
  230.         EAX - linear pointer to mem
  231.  
  232. ------------------------------------------------------------------------------
  233.   _gethimem - Get a high memory block.
  234.     In:
  235.       EAX - size requested
  236.     Out:
  237.       CF=1  - not enough mem
  238.         EAX - ?
  239.       CF=0  - memory allocated
  240.         EAX - linear pointer to mem
  241.  
  242. ------------------------------------------------------------------------------
  243.   _getmem - Allocate any mem (first cheks low, then high).
  244.     In:
  245.       EAX - size requested
  246.     Out:
  247.       CF=1  - not enough mem
  248.         EAX - ?
  249.       CF=0  - memory allocated
  250.         EAX - linear pointer to mem
  251.  
  252. ------------------------------------------------------------------------------
  253.   _lomemsize - Get amount of low mem free.
  254.     Out:
  255.       EAX - number of bytes free
  256.  
  257. ------------------------------------------------------------------------------
  258.   _himemsize - Get amount of high mem free.
  259.     Out:
  260.       EAX - number of bytes free
  261.  
  262. ------------------------------------------------------------------------------
  263.   _putdosmsg - Put a '$' terminated DOS string.
  264.     In:
  265.       EDX -> message (MUST be in low mem).
  266.  
  267.  
  268. How to use it:
  269. --------------
  270.    It's very simple, you compile START32. Compile with multiple passes to
  271. resolve forward references. Then you link it with your other 32bit modules.
  272. START32 must be the first program in the link list. Try compiling and linking
  273. the example program, then run it to make sure it runs on your system. The
  274. thing will just color the screen and pop up some numbers, press ESC to return
  275. to DOS.
  276.  
  277.  
  278. Some quickie tips on the 386 instruction set:
  279. ---------------------------------------------
  280. (This isn't anything new to those of you already familiar with it)
  281.  
  282. First the extended registers: EAX,EBX,ECX,EDX,ESI,EDI,EBP,ESP,EIP
  283. are all 32bit registers. (No you can't access EIP directly)...
  284.  
  285. The bit shift instructions, SHL, SHR, RCL, ect... accept immediate values
  286. greater than 1 now. This means 'SHL AX,5' is legal.
  287.  
  288. The addressing mode has been extended. You can use a combination of ANY 2 of
  289. the 8 general purpose instructions and an immediate value in addressing. Also
  290. one of the registers can be multiplied by 2,4, or 8 right in the address field
  291. of the instruction youre using. So 'MOV BX,[EAX+ECX*4+78]' is legal.
  292. BTW: This is really good because it allows you to use the 'LEA' instruction
  293.  to perform quick multiplies. Example: 'LEA EAX,[EAX*4+EAX]' is EAX=EAX*5.
  294.  
  295. A version of the 'IMUL' instruction doesn't require preloading registers other
  296. than the one(s) you wanna multiply... examples:
  297. IMUL EAX,71             ; EAX=EAX*71
  298. IMUL EAX,ECX,33         ; EAX=ECX*33
  299.  
  300. You can 'PUSH' immediates... 'PUSH 17983'
  301.  
  302. I don't feel like explaining the double shift SHLD/SHRD instructions or any
  303. of the BT instructions, so if you don't know em, get a book...
  304.  
  305.  
  306.  
  307. ------------------------------------------------------------------------------
  308. Anyways, play with this thing. If you like the flat memory and stuff then
  309. youre free to use this thing in whatever code you do, PD, shareware,
  310. commercial, or whatever...
  311.  
  312. Any questions or anything, you can contact me on the
  313. Sound Barrier BBS (718)979-6629 under the name 'Tom Tran'...
  314.  
  315. L8r...
  316.  
  317.